#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include "tlpi_hdr.h"

int main(int argc, char *argv[]) {
	size_t len;
	off_t offset;
	mode_t filePerms;
	int fd, ap, j, openFlags;
	char *buf;
	ssize_t numRead, numWritten;

	if(argc < 3 || 0 == strcmp(argv[1], "--help")) {
		usageErr("%s file {r<length>|R<length>|w<string>|s<offset>}...\n", argv[0]);
	}

	openFlags = O_RDWR | O_CREAT;
	filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw-rw-rw- */
	fd = open(argv[1], openFlags, filePerms); 
	if(-1 == fd) {
		errExit("open");
	}
	
	for(ap = 2; ap < argc; ap++) {
		switch(argv[ap][0]) {
			case 'r':     /* Display bytes at current offset, as text */
			case 'R':     /* Display bytes at current offset, in hex */
				len = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]);
				buf = malloc(len);
				if(NULL == buf) {
					errExit("malloc");
				}
			
				numRead = read(fd, buf, len);
				if(-1 == numRead) {
					errExit("read");
				}
		}
	}
}
